﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using V5_Model;
using V5_BLL;
using System.Data;
using System.Xml;
using V5_Common;

namespace V5_EShop._Class.Data {
    /// <summary>
    /// 网站配置
    /// </summary>
    public class V5ConfigHelper {
        string xmlConfingPath = "Config/V5Config.config";
        XmlSetHelper xmlSet = null;
        private static readonly V5ConfigHelper m_Instance = new V5ConfigHelper();
        /// <summary>
        /// 
        /// </summary>
        public static V5ConfigHelper Instance {
            get { return m_Instance; }
        }

        private DataRow _View = null;
        /// <summary>
        /// 配置字段
        /// </summary>
        public DataRow View {
            get {
                if (_View == null) {
                    _View = GetDataRow();
                    if (_View == null) {
                        throw new Exception("数据库连接失败!请检查链接字符串是否正确!");
                    }
                }
                return _View;
            }
            set { _View = value; }
        }

        public V5ConfigHelper() {
            xmlSet = new XmlSetHelper(xmlConfingPath, "configs");
            xmlSet.LoadDom();
            this.View = GetDataRow();
        }
        private string _TemplatePath;

        public string TemplatePath {
            get {
                _TemplatePath = "template/" + GetValue("TemplatePath");
                return _TemplatePath;
            }
        }

        private string _TemplateDir = "Default";

        public string TemplateDir {
            get {
                _TemplateDir = xmlSet.Items["templatedir"].ToString();
                return _TemplateDir;
            }
        }

        private BLLConfig m_bll = new BLLConfig();


        public string GetValue(string colName) {
            DataTable dt = m_bll.GetList("").Tables[0];
            return dt.Rows[0][colName].ToString();
        }



        public DataRow GetDataRow() {
            try {
                DataRow drConfig = null;
                if (DataCacheHelper.GetCache("Config") != null) {
                    drConfig = DataCacheHelper.GetCache("Config") as DataRow;
                }
                else {
                    drConfig = new BLLConfig().GetList(1, string.Empty, "ID Desc").Tables[0].Rows[0];
                    DataCacheHelper.SetCache("Config", drConfig);
                }
                return drConfig;
            }
            catch (Exception) {
                return null;
            }
        }

        #region 配置
        public string GetXmlConfigNode(string setName) {
            return xmlSet.Items[setName.ToLower()].ToString();
        }
        /// <summary>
        /// 保存节点
        /// </summary>
        public void SaveXmlConfig(string xmlPath, string xmlName, string xmlValue) {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlPath);
            XmlNodeList xmlNodeList = xmlDoc.SelectNodes("configs/" + xmlName);
            XmlNode xmlNode = xmlNodeList.Item(0);
            xmlNode.InnerText = xmlValue;
            xmlDoc.Save(xmlPath);
        }
        public void SaveXmlConfig(string xmlName, string xmlValue) {
            xmlSet.Items[xmlName] = xmlValue;
            xmlSet.SaveDom();
        }
        /// <summary>
        /// 获取节点值
        /// </summary>
        public string GetConfigValue(string nodeName) {
            try {
                if (string.IsNullOrEmpty(nodeName))
                    return "";
                return xmlSet.Items[nodeName.ToLower()].ToString();
            }
            catch (Exception) {
                return "";
            }

        }
        /// <summary>
        /// 重新载入节点
        /// </summary>
        public void ClearReSetConfig() {
            xmlSet.Items.Clear();
            xmlSet.LoadDom();
        }
        #endregion
    }
}